﻿using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace QDP.Platform.Silverlight.Tools
{
    /// <summary>
    /// 提供对参数表达式的支持。
    /// </summary>
    public sealed class ParmsExpressionHelper
    {
        /// <summary>
        /// 将参数表达式转为键值对（Dictionary 《string, string》 ）形式并返回。
        /// </summary>
        /// <param name="Expression">需要转换的表达式</param>
        /// <returns>返回转换后的键值对。</returns>
        public static Dictionary<string, string> ToDictionary(string Expression)
        {
            var result = new Dictionary<string, string>();
            if (string.IsNullOrEmpty(Expression) == false)
            {
                // 处理ParmsExpression信息
                string pattern = "(.+?)=(.+?);";
                Regex regex = new Regex(pattern);
                var matchs = regex.Matches(Expression);
                foreach (Match m in matchs)
                {
                    if (m.Success)
                    {
                        string key = m.Groups[1].Value;
                        if (result.ContainsKey(key) == false)
                        {
                            string value = m.Groups[2].Value;
                            result.Add(key, value);
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string ComineParams(params string[] args)
        {
            string ComineResult = string.Empty;
            var temp = new Dictionary<string, string>();
            foreach (string arg in args)
            {
                if (string.IsNullOrEmpty(arg) == false)
                {
                    string pattern = "(.+?)=(.+?);";
                    Regex regex = new Regex(pattern);
                    var matchs = regex.Matches(arg);
                    foreach (Match m in matchs)
                    {
                        if (m.Success)
                        {
                            string key = m.Groups[1].Value;
                            string value = m.Groups[2].Value;
                            if (temp.ContainsKey(key) == false)
                            {
                                temp.Add(key, value);
                            }
                            //else
                            //{
                            //    if (temp[key] != value )
                            //    {
                            //        temp[key] = value;
                            //    }
                            //}
                        }
                    }
                }
            }
            foreach (KeyValuePair<string, string> Entry in temp)
            {
                ComineResult += string.Format("{0}={1};", Entry.Key, Entry.Value);
            }
            return ComineResult;
        }
    }
}
